import { VNode, createVNode, render } from 'vue';
import { IPreview, IPreviewHandle, IPreviewOptions } from '@/components/preview/src/type';
import PreviewConstructor from './index.vue';

let vm: VNode;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const Preview: IPreview = function(
  opts: IPreviewOptions
) {
  const options: IPreviewOptions = <IPreviewOptions>opts;

  const container = document.createElement('div');
  container.className = 'preview-container';

  vm = createVNode(
    PreviewConstructor,
    options
  );

  render(vm, container);
  document.body.appendChild(container);
  return {
    close: options.onClose
  } as IPreviewHandle;
};

export default Preview;
